package com.project_doctor.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project_doctor.pojo.Resident;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.project_doctor.pojo.dto.ResidentSearchDTO;
import com.project_doctor.pojo.dto.ResidentTagDTO;
import com.project_doctor.pojo.ResidentTagItem;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
* @author xixif
* @description 针对表【t_resident(居民基本信息表)】的数据库操作Mapper
* @createDate 2025-06-03 16:14:08
* @Entity com.project_doctor.pojo.Resident
*/
public interface ResidentMapper extends BaseMapper<Resident> {
    List<ResidentTagDTO> selectResidents(@Param("page") Page<?> page,
                                         @Param("query") ResidentSearchDTO searchDTO);

    List<ResidentTagItem> selectResidentTags(@Param("ids") List<Long> ids);

    void insertResident(ResidentTagDTO residentTagDTO);

    Long selectResidentId();

    ResidentTagDTO selectResidentById(@Param("id") Long id);

    @Select("SELECT r.id, r.name, r.avatar, " +
            "GROUP_CONCAT(DISTINCT t.name) as healthIssues " +
            "FROM t_resident r " +
            "LEFT JOIN t_tag t ON r.id = t.ref_id AND t.ref_type = 1 " +
            "WHERE r.deleted = 0 AND t.name IS NOT NULL " +
            "GROUP BY r.id, r.name, r.avatar " +
            "ORDER BY r.id " +
            "LIMIT 3")
    List<Map<String, Object>> selectResidentsWithHealthIssues();

    @Select("SELECT COUNT(*) as total FROM t_resident WHERE deleted = 0")
    Integer getTotalResidentCount();
}




